﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using CommonIoC;


public class CanTriggerBattlePoint : MonoBehaviour
{
    [Header("是否已经触发过战斗")]
    public   bool   IsTriggeredBattle = false;
    [Header("下一个场景,中文，对应scenekey里面的")]
    public string next_scene;

    public  List<string> enemykey_list;
    

   // public DoorType battle_door;

    public string battle_key;

    public bool battle_value;



    public AudioSource audio_souce;

    public AudioClip join_battle_music;

    private SpriteRenderer spriteranderer;

    public PlayerStageManagerView playerstageview;
    void Awake()
    {
        //battle_door = DoorType.LaDuoOutCity_GoTo_FSMBattle;
        //从数据中读取，，状态
        CheckIsBattleed();
    }

    // Use this for initialization
    void Start()
    {
        // spriteranderer = GetComponent<SpriteRenderer>();
        playerstageview = GameObject.Find("PlayerManagerView").GetComponent<PlayerStageManagerView>();

    }

    void CheckIsBattleed()
    {



        //string scene_name = ""; // GameSaveLoadDataManager.Instance.join_battle_sceneName;
       List< string>  battled_keyList = BattleCreateManager.Instance.GetBattled_KeyList();


        if(battled_keyList == null)
        {
            IsTriggeredBattle = false;
        }
        else if(    battled_keyList.Contains(  battle_key))
        {
            //如果这个key包含在 战斗过的keylist里面，那么说明已经战斗过了
            IsTriggeredBattle = true;
        }

        //else if(  b_db_key == b_key)
        //{
        //    if(b_db_key == "FirstTankGou")
        //    {
        //        //发送 战狗被打败后，玩家与红狼 对话剧情
        //       // LevelManager.Instance.Send_FirstTankHongLangAsk();
        //    }

        //    IsTriggeredBattle = true;
            //spriteranderer.sprite = null;
        


    }



    // Update is called once per frame
    void Update()
    {
        if (IsTriggeredBattle)
        {
           
            Destroy(this.gameObject,0.9f);
        }
    }


    void OnTriggerEnter2D(Collider2D other)
    {
        // characterInQuicksand = true;
        //Debug.Log("TARGGERED" + NextSceneName);
        //AsyncLoadSceneCallBack.StartAsyncLoadSceneCoroutine(NextSceneName);
        //如果没有触发过战斗
        if (IsTriggeredBattle ==false)
        {

            //string this_scene_name = SceneManager.GetActiveScene().name;
      

            if (battle_key == "FirstTankGou")
            {
                //Debug.Log("进入....b_key");
              //  Vector3 firstTankGouPos = new Vector3(0.428f,-2.853f,0);
            //    LevelManager.Instance.Set_Join_Battle_Pos(firstTankGouPos );
               
               // return;
            }
            else
            {
              //  Debug.Log("进入....else了");
             //   LevelManager.Instance.Set_Join_Battle_Pos(other.gameObject.transform.position);
               
            }
            other.GetComponent<BaseControlMove>().ClosePlayerMove();

            StartCoroutine(Wait_And_PlayJoinBattleMusic_LoadScene());

        }
    }

    IEnumerator Wait_And_PlayJoinBattleMusic_LoadScene()
    {
       
        yield return new WaitForSeconds(0.2f);
        if (IsTriggeredBattle)
            yield return null;

        else
        {
            // PlayerGameStage stage = PlayerGameStage.IsDoingTalk;
            //  EventMsgHelper.SendEvent(GameEventString.OnPlayerGameStageChange, this, GameHashKeyString.PlayerGameStageOneValue, stage);
            playerstageview.TriggerJoinBattleScene();

            audio_souce.clip = join_battle_music;

            audio_souce.Play();
            BattleCreateManager.Instance.Set_BattleKey(    battle_key   );
            BattleCreateManager.Instance.AddBattledKey(    next_scene,    battle_key  );
            BattleCreateManager.Instance.SetEnemyPrefabNameListByEnemyKeyList(  enemykey_list  );

            SceneLoadManager.Instance.Set_Prev_Door( this.next_scene );
            SceneLoadManager.Instance.Set_Join_BattleSceneName(  SceneManager.GetActiveScene().name );

            SceneLoadManager.Instance.Set_Join_Battle_Pos(  this.transform.position );

         yield return new WaitForSeconds(0.8f);


            IsTriggeredBattle = true;
           // GameObject player = GameObject.Find("GameMainPlayer");

            

           // LevelManager.Instance.Set_Join_Battle_Pos(player.transform.position);
            yield return new WaitForSeconds(0.1f);
            // SceneManager.LoadScene(battle_scene_name);
            SceneLoadManager.Instance.LoadNext(next_scene);


            Debug.Log("准备进入战斗场景...");
        }
    }


  

}
